﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FancyBattles.GameEntities;

namespace FancyBattles.GameScreen.StateMachine
{
    class DefineShotState : State
    {

        private static Texture2D BLUE_BOW_TEXTURE;
        private static Texture2D RED_BOW_TEXTURE;
        private BouncingBitch selectedBitch;
        private float angleRotation;

        private Vector2 shotDirection = Vector2.Zero;

        public static Texture2D BlueBow
        {
            set { BLUE_BOW_TEXTURE = value; }
        }

        public static Texture2D RedBow
        {
            set { RED_BOW_TEXTURE = value; }
        }

        public DefineShotState(GameplayScreen parentScreen, PlayerIndex player)
            : base(parentScreen, player)
        {
            selectedBitch = (from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First();
        }

        public override void Update(GameTime gameTime)
        {
            angleRotation = (float)(Math.Atan2(shotDirection.X, -shotDirection.Y)) + MathHelper.ToRadians(90);
            if (!double.IsNaN(InputHandler.Direction(player).X))
            {
                shotDirection = InputHandler.Direction(player);
            }
            if (InputHandler.isConfirm(player))
            {
                selectedBitch = (from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First();

                foreach (Arrow arrow in (from ar in Arrow.ArrowList where ar.Bitch.Equals(selectedBitch) select ar).ToList())
                {
                    Arrow.ArrowList.Remove(arrow);
                }

                Arrow newArrow = new Arrow(shotDirection, selectedBitch);
                ParentScreen.ScreenStates[player] = new DefineMoveState(ParentScreen, player);
            }            
            if (InputHandler.isCancel(player))
            {
                ParentScreen.ScreenStates[player] = new SelectState(ParentScreen, player);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (player.Equals(PlayerIndex.One))
            {
                ParentScreen.ScreenManager.SpriteBatch.Draw(BLUE_BOW_TEXTURE, new Vector2(selectedBitch.Position.X + 15, selectedBitch.Position.Y + 15), null, Color.White, angleRotation, new Vector2(87, 43), 1, SpriteEffects.None, 0);
            }
            else
            {
                ParentScreen.ScreenManager.SpriteBatch.Draw(RED_BOW_TEXTURE, new Vector2(selectedBitch.Position.X + 15, selectedBitch.Position.Y + 15), null, Color.White, angleRotation, new Vector2(87, 43), 1, SpriteEffects.None, 0);
            }
            //selectedBitch = (from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First();
        }
    }
}
